<template>
  <div class="app-container">
    <!--工具栏-->
<div class="head-container">
         <!--  <div v-if="crud.props.searchToggle">

        <el-input v-model="query.name" clearable size="small" placeholder="输入部门名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker v-model="query.createdTime" class="date-item" />
        <el-select v-model="query.enabled" clearable size="small" placeholder="状态" class="filter-item" style="width: 90px" @change="crud.toQuery">
          <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation />
      </div> -->
      <crudOperation :permission="permission" />
    </div>
    <!--表单组件-->
    <!-- <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;" />
        </el-form-item>

        <el-form-item label="顶级部门">
          <el-radio-group v-model="form.isTop" style="width: 140px">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item v-if="form.isTop === '0'" style="margin-bottom: 0;" label="上级部门" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :load-options="loadDepts"
            :options="depts"
            style="width: 370px;"
            placeholder="选择上级类目"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog> -->
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.datas"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >

      <el-table-column type="selection"  width="55" />
      <el-table-column   label="名称" prop="label"
       />
      <el-table-column   label="排序" prop="t.deptSort"
     />
        <el-table-column width="120px" label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.t.enabled"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>

      <!-- <el-table-column label="状态" align="center" prop="enabled">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            :disabled="scope.row.id === 1"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.enabled,)"
          />
        </template>
      </el-table-column> -->
      <el-table-column prop="createdTime" label="创建日期" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.t.createdTime) }}</span>
        </template>
      </el-table-column>

       <el-table-column
            v-permission="['admin','dept:edit','dept:del']"
            label="操作"
            width="115"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <udOperation
                :data="scope.row"
                :permission="permission"
                 :disabled-dle="scope.row.id === 1"
            msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！"
              />

             </template>
          </el-table-column>
    </el-table>
  </div>
</template>

<script>
import crudDept from '@/api/organization/group'

import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import CRUD, { presenter, header, form, crud } from '../../../components/Crud/crud'
import rrOperation from '@/components/Crud/RR.operation'
import crudOperation from '@/components/Crud/CRUD.operation'
import udOperation from '@/components/Crud/UD.operation'
import pagination from '@/components/Crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, name: null, isTop: '1', subCount: 0, parentId: null, deptSort: 999, enabled: 'true' }
export default {
  name: 'GroupManagement',
  components: { Treeselect, crudOperation, rrOperation, udOperation, DateRangePicker },
  cruds() {
    return CRUD({ title: '部门', url: '/organization/group', crudMethod: { ...crudDept }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 设置数据字典
  dicts: ['dept_status'],
  data() {
    return {
      depts: [],
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        deptSort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ]
      },
      permission: {
        add: ['admin', 'dept:add'],
        edit: ['admin', 'dept:edit'],
        del: ['admin', 'dept:del']
      },
      enabledTypeOptions: [
        { key: 'true', display_name: '正常' },
        { key: 'false', display_name: '禁用' }
      ]
    }
  },

  methods: {

    // getDeptDatas(tree, treeNode, resolve) {
    //   const params = { parentId: tree.id }
    //   setTimeout(() => {
    //     crudDept.getDepts(params).then(res => {

    //       resolve(res.content)
    //     })
    //   }, 100)
    // },
    // 新增与编辑前做的操作
    // [CRUD.HOOK.afterToCU](crud, form) {
    //   if (form.parentId !== null) {
    //     form.isTop = '0'
    //   } else if (form.id !== null) {
    //     form.isTop = '1'
    //   }
    //   form.enabled = `${form.enabled}`
    //   if (form.id != null) {
    //     this.getSupDepts(form.id)
    //   } else {
    //     this.getDepts()
    //   }
    // },
    // getSupDepts(id) {
    //   crudDept.getDeptSuperior(id).then(res => {
    //     const date = res.content
    //     this.buildDepts(date)
    //     this.depts = date
    //   })
    // },
    // buildDepts(depts) {
    //   depts.forEach(data => {
    //     if (data.children) {
    //       this.buildDepts(data.children)
    //     }
    //     if (data.hasChildren && !data.children) {
    //       data.children = null
    //     }
    //   })
    // },
    // getDepts() {
    //   crudDept.getDepts({ enabled: true }).then(res => {
    //     this.depts = res.content.map(function(obj) {
    //       if (obj.hasChildren) {
    //         obj.children = null
    //       }
    //       return obj
    //     })
    //   })
    // },
    // 获取弹窗内部门数据
    // loadDepts({ action, parentNode, callback }) {
    //   if (action === LOAD_CHILDREN_OPTIONS) {
    //     crudDept.getDepts({ enabled: true, parentId: parentNode.id }).then(res => {
    //       parentNode.children = res.content.map(function(obj) {
    //         if (obj.hasChildren) {
    //           obj.children = null
    //         }
    //         return obj


    //       })
    //       setTimeout(() => {
    //         callback()
    //       }, 100)
    //     })
    //   }
    // },
    // 提交前的验证
    // [CRUD.HOOK.afterValidateCU]() {
    //   if (this.form.parentId !== null && this.form.parentId === this.form.id) {
    //     this.$message({
    //       message: '上级部门不能为空',
    //       type: 'warning'
    //     })
    //     return false
    //   }
    //   if (this.form.isTop === '1') {
    //     this.form.parentId = null
    //   }
    //   return true
    // },
    // 状态修改
    // handleStatusChange(row) {
    //   const text = row.t.enabled === true ? '启用' : '停用'
    //   this.$confirm('确认要"' + text + '""' + row.t.name + '"部门吗?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function() {
    //     return changeDeptEnabled(row.id, row.t.enabled)
    //   }).then(() => {
    //     this.$notify({
    //       title: '编辑成功',
    //       message: '编辑成功',
    //       type: 'success',
    //       duration: 2000
    //     })
    //   }).catch(function() {
    //     row.t.enabled = row.t.enabled === false
    //   })
    // },

    // checkboxT(row, rowIndex) {
    //   return row.id !== 1
    // }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
 ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
